﻿Imports MySql.Data.MySqlClient
Public Class frmLicense
    Dim Mysql1 As New ClsSQLhelper
    Private Sub frmLicense_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim LicenseCode As String
        Dim key As String
        Dim hcode As String
        Dim UserMode As String

        txtGUID.Text = System.Guid.NewGuid.ToString()
        LicenseCode = ClsSQLhelper.License
        UserMode = ClsSQLhelper.UserMode
        If UserMode = "1" Then
            txtHospCode.Visible = False
            txtCode.Visible = False
            cmdGenKey.Visible = False
            cmdAdd.Visible = False
            Label1.Visible = False
        ElseIf UserMode = "2222" Then
            txtHospCode.Visible = True
            txtCode.Visible = True
            cmdGenKey.Visible = True
            cmdAdd.Visible = True
            Label1.Visible = True
        Else
            txtHospCode.Visible = False
            txtCode.Visible = False
            cmdGenKey.Visible = False
            cmdAdd.Visible = False
            Label1.Visible = False
        End If

        If LicenseCode = "" Then
            MsgBox("ตรวจพบการตั้งค่าคีย์ไม่ถูกต้อง กรุณาติดต่อ Admin", MsgBoxStyle.Information)
        Else
            key = "sys11266"
            hcode = Crypto.Decrypt(ClsSQLhelper.License, key)
            txtGenCode.Text = LicenseCode

            Dim DS1 As New DataSet
            Dim strSql1 As String
            Dim Mysql1 As New ClsSQLhelper
            '------------------------
            strSql1 = "select hospitalcode,hospitalname from opdconfig  "
            DS1 = Mysql1.GetMYSQLDataset(strSql1, "ds_hosname", "opdconfig")

            If DS1.Tables(0).Rows.Count > 0 Then
                If DS1.Tables(0).Rows(0).Item("hospitalcode").ToString() = "" Then
                    MsgBox("ตรวจพบการตั้งค่าไม่ถูกต้อง กรุณาติดต่อ Admin", MsgBoxStyle.Information)
                    'End

                Else
                    txtHospCode.Text = DS1.Tables(0).Rows(0).Item("hospitalcode").ToString()

                    If txtHospCode.Text <> hcode Then
                        MsgBox("ตรวจพบการตั้งค่าคีย์ไม่ถูกต้อง กรุณาติดต่อ Admin", MsgBoxStyle.Information)
                        lblHospname.Text = "License to Incorrect "
                    Else

                        lblHospname.Text = "License to " + DS1.Tables(0).Rows(0).Item("hospitalcode").ToString() + "  " + DS1.Tables(0).Rows(0).Item("hospitalname").ToString()
                        'Me.Hide()
                        'frmBroker.Show()

                    End If
                End If

            End If
            '------------------------

        End If

    End Sub

    Private Sub cmdGenKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenKey.Click
        If (txtHospCode.Text <> "" And txtCode.Text <> "") Then
            txtGenCode.Text = Crypto.Encrypt(txtHospCode.Text, txtCode.Text)
        Else
            MsgBox("กรุณากรอก Code", MsgBoxStyle.Exclamation)
        End If
    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click


        If txtGenCode.Text <> "" Then

            ClsSQLhelper.License = Trim(txtGenCode.Text)
            MsgBox("บันทึกค่าสำเร็จ กรุณาปิดแล้วเปิดโปรแกรมใหม่เพื่อให้โปรแกรมใช้ค่าที่ตั้งใหม่", MsgBoxStyle.Information)
            End
        End If



    End Sub
End Class